﻿using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmWordToImg : Form
    {
        public FrmWordToImg()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择Word文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiles = new OpenFileDialog();
            openFiles.Multiselect = true;
            openFiles.Filter = "word文档|*.docx;*.doc";
            if (openFiles.ShowDialog() == DialogResult.OK)
            {
                string[] filenames = openFiles.FileNames;
                Array.Sort(filenames);
                foreach (string filename in filenames)
                {
                    lstBoxFileList.Items.Add(filename);
                }
            }
        }

        /// <summary>
        /// 选择保存图片的文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (lstBoxFileList.Items.Count < 1)
            {
                MessageBox.Show("请添加要转换的word文档！");
                return;
            }
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = folderBrowser.SelectedPath;
            }
        }

        /// <summary>
        /// 开始Word转图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstBoxFileList.Items.Count < 1)
            {
                MessageBox.Show("请添加要操作的word文档！");
                return;
            }
            if (string.IsNullOrEmpty(txtSavePath.Text))
            {
                MessageBox.Show("请选择保存路径！");
                return;
            }
            if (cmbImgFormat.SelectedItem == null)
            {
                MessageBox.Show("请选择图片格式！");
                return;
            }
            string imgFormat = "png";
            ImageFormat imageFormat = ImageFormat.Png;
            switch (cmbImgFormat.SelectedText)
            {
                case "jpg":
                    imgFormat = "jpg";
                    imageFormat = ImageFormat.Jpeg;
                    break;
                case "png":
                    imgFormat = "png";
                    imageFormat = ImageFormat.Png;
                    break;
                case "bmp":
                    imgFormat = "bmp";
                    imageFormat = ImageFormat.Bmp;
                    break;
                default:
                    imgFormat = "png";
                    break;
            }
            foreach (var item in lstBoxFileList.Items)
            {
                string fileName = item.ToString();
                //创建 Document 类的对象
                Document doc = new Document();
                //载入Word文档
                doc.LoadFromFile(fileName);

                Image[] images;
                if (imgFormat == "png")
                {
                    //将整个Word文档转换为位图集合(png)
                    images = doc.SaveToImages(ImageType.Metafile);
                }
                else
                {
                    //将整个文档的页面分别转换为图片
                    images = doc.SaveToImages(ImageType.Bitmap);
                }

                //在转换出的图片集合中循环
                for (int i = 0; i < images.Length; i++)
                {
                    Image newImage = images[i];
                    if (imgFormat == "png")
                    {
                        //设置图片的分辨率
                        newImage = ResetResolution((Metafile)newImage, 300);
                    }

                    //将图片保存的图片
                    string outputFileName = $"{Path.GetFileNameWithoutExtension(fileName)}_{i}.{imgFormat}";
                    string outputFilePath = Path.Combine(txtSavePath.Text, outputFileName);
                    newImage.Save(outputFilePath, imageFormat);
                }
            }

            MessageBox.Show("完成！");
        }

        /// <summary>
        /// 设置图片分辨率
        /// </summary>
        /// <param name="mf"></param>
        /// <param name="resolution">分辨率数值大小</param>
        /// <returns></returns>
        public static System.Drawing.Image ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(mf, Point.Empty);
            }
            return bmp;
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
